<script>
    const TYPE_MINE = 1;
    const TYPE_SHARE = 2;
    var g_pid = null;

    var router = new Router({ignorecase: true});

    var myScroll = null;
    var PAGE_TYPE_HOME = 1;
    var PAGE_TYPE_SHARE = 2;

    router.href = function (path) {
        window.location.href = "#" + path;
    }

    router.before(function (req, next) {
        $(".page.active")
                .removeClass("active")
                .fadeOut(function () {
                    if ($(this).hasClass("active")) return;
                    $(this).hide();
                });
        $(".dialog").removeClass("active").hide();
        next();
    });

    //    router.addRoute('#/*', function (req, next) {
    //
    //    });
    //默认地址
    router.addRoute('#/', function () {
        var type = getQueryString("type");
        if (type == PAGE_TYPE_SHARE) {
            router.href("/share");
        } else {
            router.href("/home");
        }
    });

    router.addRoute('#/rule', function () {
        $("#rule")
                .addClass("active")
                .fadeIn(function () {
                    if (!myScroll) {
                        myScroll = new IScroll('#rule .wrapper', {
                            bounce: false
                        });
                    }
                });
    });

    router.addRoute('#/home', function () {
        $("#home")
                .addClass("active")
                .fadeIn();
        getMineShareContent();
    });
    router.addRoute('#/rd', function () {
        router.href("/home");
    });
	router.addRoute('#rd', function () {
        router.href("/home");
    });
    router.addRoute('#/mine', function () {
        $("#mine")
                .addClass("active")
                .fadeIn();
        getMineNum();
        getMineShareContent();
    });

    router.addRoute('#/share', function () {
        $("#share")
                .addClass("active")
                .fadeIn();
        getShareNum();
        getFriendShareContent();
    });

    var getMineNum = function () {
        getNum(TYPE_MINE, null, function (data) {
            fillPageByNum($("#mine"), data);
        });
    }


    var getShareNum = function (cb) {
        var openid = getQueryString("q_openid");
        getNum(TYPE_SHARE, openid, function (data) {
            fillPageByNum($("#share"), data);
            cb && cb();
        });
    }

    var fillPageByNum = function ($el, data) {
        var result = data.result;
        if (result["1"].already_num < result["1"].num) {
            g_pid = 1;
        } else if (result["2"].already_num < result["2"].num) {
            g_pid = 2;
        } else if (result["3"].already_num < result["3"].num) {
            g_pid = 3;
        }

        $el.find(".prize_status .i-1 .num .count").html(result["1"].already_num);
        $el.find(".prize_status .i-1 .num .sum").html(result["1"].num);

        $el.find(".prize_status .i-2 .num .count").html(result["2"].already_num);
        $el.find(".prize_status .i-2 .num .sum").html(result["2"].num);

        $el.find(".prize_status .i-3 .num .count").html(result["3"].already_num);
        $el.find(".prize_status .i-3 .num .sum").html(result["3"].num);

        var userinfo = data.userinfo;
        if (userinfo && userinfo.nick)
            $el.find(".word_area .content span").html(userinfo.nick);
    }

    var getNum = function (type, openid, cb) {
        $.ajax({
            url: "/index/pumpUp/getNum",
            type: "POST",
            data: {
                type: type,
                openid: openid
            },
            dataType: "json",
            success: function (data) {
                if (data.errno) {
                    alert(data.errdesc);
                } else {
                    cb && cb(data.data);
                }
            },
            error: function () {
                alert("网络错误");
            }
        })
    }


</script>